﻿using System;
using System.Diagnostics;

namespace SfsTools.Core
{
    /// <summary>
    /// Represents a continuation index area entry.
    /// </summary>
    internal class ContinuationEntry : IndexEntry
    {
        #region Properties

        /// <summary>
        /// Gets the name of the directory or file.
        /// </summary>
        public Byte[] Name
        {
            [DebuggerStepThrough]
            get
            {
                return FBuffer;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Copies the contents of this entry to <paramref name="buffer"/>.
        /// </summary>
        /// <param name="buffer">The buffer to copy the contents to.</param>
        /// <param name="index">The starting byte index.</param>
        [DebuggerStepThrough]
        public void CopyTo(byte[] buffer, int index)
        {
            Array.Copy(FBuffer, 0, buffer, index, FBuffer.Length);
        }

        /// <summary>
        /// Copies the contents of <paramref name="buffer"/> to this entry.
        /// </summary>
        /// <param name="buffer">The buffer to copy the contents from.</param>
        /// <param name="index">The starting byte index.</param>
        [DebuggerStepThrough]
        public void CopyFrom(byte[] buffer, int index)
        {
            Array.Copy(buffer, index, FBuffer, 0, Math.Min(FBuffer.Length, buffer.Length - index));
            // (bug) see issues.txt, this is due to an error in the specification.
            Debug.Assert(FBuffer[0] != 0x00, "Continuation entry starts with zero byte");
            // Normal assertion.
            Debug.Assert(FBuffer[0] >= 0x20, "Continuation entry starts with byte <32");
        }

        /// <summary>
        /// Accepts a visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        [DebuggerStepThrough]
        public override void AcceptVisitor(IIndexEntryVisitor visitor)
        {
            visitor.VisitContinuation(this);
        }

        #endregion

        #region Constructors
        /// <overloads>Initializes a new instance of <see cref="UnknownIndexEntry"/>.</overloads>
        /// <summary>
        /// Initializes a new instance of <see cref="UnknownIndexEntry"/>.
        /// </summary>
        [DebuggerStepThrough]
        public ContinuationEntry(long position, byte[] buffer)
            : base(position, buffer)
        {
        }
        #endregion
    }
}
